<?php

class Model extends PDO {
        
    public $servidor = "localhost";
    public $usuario = "root";
    public $senha = "";
    public $nomeBanco = "4task";
    public $dataset = NULL;
    public $conexao = NULL;
    public $linhasAfetadas = -1;
    public $type = 'mysql';
    public $porta = '3306';
    private $tabela = '';
    
    public function setTabela($tabela){
        $this->tabela = $tabela;
    }
    
    public function getTabela(){
        return $this->tabela;
    }
    
    public function __construct($tabela) {
        $this->tabela = $tabela;
        $this->conectar();
        if($this->conexao==null){
            echo 'Erro ao conectar';
        }
    }

    public function __destruct() {
        $this->conexao = null;
    }

    public function conectar(){
        $dsn = "mysql:host={$this->servidor};port={$this->porta};dbname={$this->nomeBanco}";
        $opcoes = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_CASE => PDO::CASE_LOWER
        );
        
        try{
            $this->conexao = new PDO($dsn, $this->usuario, $this->senha, $opcoes);
        }catch (PDOException $e) {
            echo 'Erro: '.$e->getMessage();
        }
    }    
    
    public function desconectar() {
        $this->__destruct();
    }
    
    public function insert($campos,$valores){
        $sql = "insert into {$this->tabela} (";    
        foreach ($campos as $key => $value):
             $aux = array_keys($campos);
             if ($key == end($aux)){
                 $sql.= "{$campos[$key]} ) ";
             }
             else{
                 $sql.= "{$campos[$key]}, ";
             }
        endforeach;
        $sql .=" values ( ";    
        foreach ($valores as $key => $value):
             $aux = array_keys($valores);
             if ($key == end($aux)){
                 $sql.= " ? ) ";
             }
             else{
                 $sql.= "?, ";
             }
        endforeach;
        try{
            $this->conectar();
            $stmt = $this->conexao->prepare($sql);
            $stmt->execute($valores);   
        }
        catch (PDOException $e) {
            echo 'Erro: '.$e->getMessage();
        }
        return $this->conexao->lastInsertId();
        $this->desconectar();
    }
    
    public function update($campos, $valores, $criteria){
       $sql = "UPDATE {$this->tabela} SET ";    
        foreach ($campos as $key => $value):
             $aux = array_keys($campos);
             if ($key == end($aux)){
                 $sql.= "{$campos[$key]} = ? ";
             }
             else{
                 $sql.= "{$campos[$key]} = ? , ";
             }
        endforeach;
        $sql.= "WHERE $criteria ";
        $this->conectar();
        $stmt = $this->conexao->prepare($sql);
        return $stmt->execute($valores);
        $this->desconectar();
    }
    
    public function select($criteria){
        $sql = "SELECT * FROM {$this->tabela} WHERE {$criteria}";
        $this->conectar();
        $stmt = $this->conexao->query($sql);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        $this->desconectar();
        return $result;
    }
    
    public function selectAll(){
        $sql = "SELECT * FROM {$this->tabela}";
        $this->conectar();
        $stmt = $this->conexao->query($sql);
        while ($lin = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $result[] = $lin;    
        }
        $this->desconectar();
        return $result;
    }
    
    public function delete($criteria){
        $sql = "DELETE FROM {$this->tabela} WHERE {$criteria}";    
        $this->conectar();
        return $this->conexao->exec($sql);
    }
    
    public function executa($sql){
        $this->conectar();
        return $this->conexao->query($sql);
    }
}



?>